﻿using System;
using System.Collections;
using System.Configuration;
using System.Net;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Theme_3_testIP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetUserLocation002();
        Response.Write(GetUserLocation());
        Response.End();
    }

    public  string GetUserLocation()
    {
        try
        {
            WebClient webGetting = new WebClient();
            string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
            string startString = @"来自：";
            int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
            int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
            return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
        }
        catch
        {
            return string.Empty;
        }
    }

    public void GetUserLocation002()
    {
        try
        {
            WebClient webGetting = new WebClient();
            string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            Response.Write("userIP=");
            Response.Write(userIP);
            Response.Write("<br />");
            string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
            Response.Write("ipQueryResult=");
            Response.Write(ipQueryResult);
            Response.Write("<br />");
            string startString = @"来自：";
            int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
            Response.Write("startIndex=");
            Response.Write(startIndex);
            Response.Write("<br />");
            int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
            Response.Write("endIndex=");
            Response.Write(endIndex);
            Response.Write("<br />");
            Response.Write("ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex)=");
            Response.Write(ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex));
            Response.Write("<br />");
            //return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
        }
        catch
        {
            //return string.Empty;
        }
    }

}
